package pl.register.android.widget.table;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.TagLesson;
import pl.register.android.widget.Refreshable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-20
 */
public abstract class StudentLessonsTableAdapter {

    protected LayoutInflater inflater;

    protected Context context;

    protected Resources resources;

    private Refreshable refreshable;

    private List<TagLesson> tags;

    private Map<TagLesson, List<Lesson>> lessonsMap;

    public abstract View getLessonSectionView(View view, int tagPosition);

    public StudentLessonsTableAdapter(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
        this.resources = context.getResources();
        this.tags = TagLesson.asList();
        this.lessonsMap = createDefaultMap();
    }

    public Map<TagLesson, List<Lesson>> createDefaultMap() {
        Map<TagLesson, List<Lesson>> lessonsMap = new HashMap<TagLesson, List<Lesson>>();
        for (TagLesson tag : tags) {
            lessonsMap.put(tag, new ArrayList<Lesson>());
        }
        return lessonsMap;
    }

    public int getCountSection() {
        return tags.size();
    }

    protected TagLesson getTag(int tagPosition) {
        return tags.get(tagPosition);
    }

    protected List<Lesson> getLessonsBy(TagLesson tag) {
        return lessonsMap.get(tag);
    }

    public void setRefreshable(Refreshable refreshable) {
        this.refreshable = refreshable;
    }

    public void refreshItems(List<TagLesson> tags, Map<TagLesson, List<Lesson>> lessonsMap) {
        this.tags = tags;
        this.lessonsMap = lessonsMap;
        if (refreshable != null) {
            refreshable.refresh();
        }
    }

}